package secondriver.heathfood.util;

import android.content.Context;
import android.os.Environment;
import secondriver.heathfood.client.Constants;

import java.io.*;

import static android.os.Environment.MEDIA_MOUNTED;

/**
 * Author : secondriver
 * Date :  2015/11/2
 */
public class FileUtil {


    public static void writeToFile(String fileName, String content, Context context) {
        FileOutputStream fos = null;
        try {
            fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            fos.write(content.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static boolean fileIsExist(String fileName, Context context) {
        File fileDir = context.getFilesDir();
        File file = new File(fileDir, fileName);
        return file.exists();
    }

    public static void deleteFile(String fileName, Context context) {
        File fileDir = context.getFilesDir();
        File file = new File(fileDir, fileName);
        if (file.exists()) {
            file.delete();
        }
    }

    public static String readFromFile(String fileName, Context context) {
        FileInputStream fin = null;
        ByteArrayOutputStream bos = null;
        try {
            fin = context.openFileInput(fileName);
            byte[] buf = new byte[1024];
            int len = 0;
            bos = new ByteArrayOutputStream();
            while ((len = fin.read(buf)) != -1) {
                bos.write(buf, 0, len);
            }
            return bos.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bos) {
                    bos.close();
                }
                if (null != fin) {
                    fin.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static boolean isUsableExternalStorage() {
        return Environment.getExternalStorageState().equals(MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable();
    }

    public static File createAppExternalPublicPictureDirectory() {
        File publicPictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File heathPictureDir = new File(publicPictureDir, Constants.SCREEN_SHOT_DIRECTORY);
        if (!heathPictureDir.exists()) {
            heathPictureDir.mkdirs();
        }
        if (heathPictureDir.exists()) {
            return heathPictureDir;
        }
        return null;
    }

    public static String generatorScreenShotPictureName() {
        return Constants.SCREEN_SHOT_PREFIX + System.currentTimeMillis() + ".png";
    }
}
